package com.sort;

public class TrainSort {
    public static void main(String[] args) {
        int[] num = {1,4,6,2,6,3};
//        selectSort(num);

        quickSort(num, 0, num.length-1);
        for (int i : num) {
            System.out.print(i+" ");
        }
    }

    private static void quickSort(int[] num, int start, int end) {
        if(start >= end) return;
        int pivot = num[end];
        int i = start -1; //用于追踪小于基准元素的区域的末尾。
        for (int j = start; j < end; j++) {
            if(num[j] < pivot){
                i++;
                swap(num,i,j);
            }
        }

        swap(num,i+1,end);

        quickSort(num, start, i);
        quickSort(num, i + 2, end);

    }

    private static void swap(int[] num, int i, int j) {
        int temp = num[i];
        num[i] = num[j];
        num[j] = temp;
    }

    private static void selectSort(int[] num) {
        int n = num.length;

        for (int i = 0; i < n; i++) {
            int min = num[i];
            int minIndex = i;

            for (int j = i+1; j < n; j++) {
                if(num[j] < min){
                    min = num[j];
                    minIndex = j;
                }
            }

            int temp = num[i];
            num[i] = num[minIndex];
            num[minIndex] = temp;
        }
    }
}
